package cn.com.connext.scrm.service;

import cn.com.connext.scrm.domain.master.SupportThirdInterface;
import cn.com.connext.scrm.repository.master.SupportThirdInterfaceRepository;
import cn.com.connext.scrm.service.dto.JsonResult;
import cn.com.connext.scrm.service.mapper.SupportThirdInterfaceService;
import cn.com.connext.scrm.utils.DateUtils;
import com.connext.common.util.DateUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.ZonedDateTime;
import java.util.*;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

@Service
public class SupportThirdInterfaceServiceImpl implements SupportThirdInterfaceService {
    private static final Logger logger = LoggerFactory.getLogger(SupportThirdInterfaceServiceImpl.class);

    @Autowired
    private SupportThirdInterfaceRepository thirdInterfaceRepository;
//    @Override
//    public JsonResult addProduct(List<SupportThirdInterface> list) {
//        JsonResult jsonResult = new JsonResult();
//        List<SupportThirdInterface> resultList = new ArrayList<>();
//        if(list !=null && list.size() >0){
//            for (SupportThirdInterface sti:resultList) {
//                SupportThirdInterface exsitEntity = thirdInterfaceRepository.findByUniqueCodeAndSkuId(sti.getUniqueCode(),sti.getSkuId());
//                if(exsitEntity ==null){
//                    if(StringUtils.isBlank(sti.getUniqueCode())){
//                        jsonResult.setState(1);
//                        jsonResult.setMessage("唯一码不能为空！");
//                        return  jsonResult;
//                    }else if(StringUtils.isBlank(sti.getSkuId())){
//                        jsonResult.setState(1);
//                        jsonResult.setMessage("skuId不能为空！");
//                        return  jsonResult;
//                    }else{
//                        sti.setCreateTime(ZonedDateTime.now());
//                        resultList.add(sti);
//                    }
//                }
//            }
//        }
//        if(resultList.size() >0){
//            logger.info("本次添加总数据"+resultList.size());
//            thirdInterfaceRepository.save(resultList);
//            jsonResult.setState(0);
//            jsonResult.setMessage("添加成功");
//        }else{
//            logger.info("本次无有效数据");
//            jsonResult.setState(1);
//            jsonResult.setMessage("无效数据");
//        }
//        return  jsonResult;
//    }

    @Override
    public Integer isActivate(String uniqueCode) {
        Integer count = thirdInterfaceRepository.findIsExist(uniqueCode);
        return count;
    }

    @Override
    public SupportThirdInterface findByUniqueCode(String uniqueCode) {
        return thirdInterfaceRepository.findByUniqueCode(uniqueCode);
    }

    @Override
    public JsonResult findCountByCreateTime(Date createTime) {
        JsonResult result =new JsonResult();
        String format = DateUtil.format(createTime, "yyyy-MM-dd");
        Integer count = thirdInterfaceRepository.findCountByCreateTime(format);
        int totalCount = count == null ? 0 : count;
        result.setData(totalCount);
        result.setState(0);
        result.setMessage(format+"总添加数据"+totalCount+"条");
        return result;
    }


    @Override
    public JsonResult addProduct(List<SupportThirdInterface> list) {
        JsonResult jsonResult = new JsonResult();
        List<SupportThirdInterface> resultList = new ArrayList<>();
        if(list !=null && list.size() >0){
            List<String> uniqueCodes = new ArrayList<>();
            for (SupportThirdInterface sti:list) {
                logger.info("本条uniqueCode"+sti.getUniqueCode());
                if(StringUtils.isBlank(sti.getUniqueCode())){
                    jsonResult.setState(1);
                    jsonResult.setMessage("唯一码不能为空！");
                    return  jsonResult;
                }else if(StringUtils.isBlank(sti.getSkuId())){
                    jsonResult.setState(1);
                    jsonResult.setMessage("skuId不能为空！");
                    return  jsonResult;
                }else{
                    uniqueCodes.add(sti.getUniqueCode());
                    sti.setCreateTime(new Date());
                    resultList.add(sti);
                }
            }
            //查询传入的所有码在数据库已存在的
            List<SupportThirdInterface> allList = thirdInterfaceRepository.findByCondition(uniqueCodes);
            if(allList.size() >0){
                //从传入所有码中删除已存在的所有码，剩余不重复的
                resultList.removeAll(allList);
            }
        }
        if(resultList.size() >0){
            logger.info("本次添加总数据"+resultList.size());
            //开启新线程执行批量添加操作
            ExecutorService service = Executors.newSingleThreadExecutor();
            service.execute(() -> {
                thirdInterfaceRepository.save(resultList);
            });
            jsonResult.setState(0);
            jsonResult.setMessage("添加成功");
        }else{
            logger.info("本次无有效数据");
            jsonResult.setState(1);
            jsonResult.setMessage("无效数据");
        }
        return  jsonResult;
    }

}
